package com.future.hedongjun.mine.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.GlideUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.common.PriceHelper;
import com.future.hedongjun.home.bean.ClassDetail;
import com.future.hedongjun.mall.activity.CouponListActivity;
import com.future.hedongjun.mall.bean.GoodsDetail;
import com.future.hedongjun.mine.bean.DeliveryAddressBean;
import com.future.hedongjun.mine.bean.response.CouponListResponse;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

public class ConfirmClassOrderActivity extends BaseActivity {

    @BindView(R.id.iv_class)
    ImageView ivClass;
    @BindView(R.id.tv_class_title)
    TextView tvClassTitle;
    @BindView(R.id.tv_class_price)
    TextView tvClassPrice;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.layout_coupon)
    FrameLayout layoutCoupon;
    @BindView(R.id.tv_price_commodity)
    TextView tvPriceCommodity;
    @BindView(R.id.layout_price_commodity)
    FrameLayout layoutPriceCommodity;
    @BindView(R.id.tv_coupon_price)
    TextView tvCouponPrice;
    @BindView(R.id.btn_share)
    Button btnShare;
    @BindView(R.id.tv_share_tip)
    TextView tvShareTip;
    @BindView(R.id.tv_should_pay)
    TextView tvShouldPay;
    @BindView(R.id.tv_should_pay_price)
    TextView tvShouldPayPrice;
    @BindView(R.id.btn_pay)
    Button btnPay;
    private ClassDetail detail;
    private double couponPrice = 0;
    private String coupon_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_class_order);
        ButterKnife.bind(this);

        GlideUtils.loadImage(this, detail.getLogo_img(), ivClass);
        tvClassTitle.setText(detail.getName());
        tvClassPrice.setText(PriceHelper.priceFormat(detail.getPrice()));

        getCanUseCouponCount();
    }

    private void refreshPrice() {
        tvPriceCommodity.setText(PriceHelper.priceFormat(detail.getPrice()));
        tvCouponPrice.setText("- " + PriceHelper.priceFormat(couponPrice));
        tvShouldPayPrice.setText(PriceHelper.priceFormat(detail.getPrice() - couponPrice));
    }

    private void getCanUseCouponCount() {
        new HttpRequest<CouponListResponse>()
                .with(this)
                .setApiCode("_canusediscount_001")
                .addParam("userToken", MyApp.getInstance().getUserToken())
                .addParam("amount", "" + detail.getPrice())
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        tvCoupon.setText(((CouponListResponse) response).data.size() + "张可用");
                    }
                }).start(new CouponListResponse());
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        detail = (ClassDetail) extras.getSerializable("data");
    }

    public static void open(Context context, ClassDetail detail) {
        Intent intent = new Intent(context, ConfirmClassOrderActivity.class);
        intent.putExtra("data", detail);
        context.startActivity(intent);
    }

    @OnClick({R.id.btn_share, R.id.btn_pay, R.id.layout_coupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_share:
                //课程分享
                new HttpRequest<DefaultResponse>()
                        .with(this)
                        .setApiCode("")
                        .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                            @Override
                            public void onSuccess(BaseResponse response) {
                                showShare("标题", "内容", "链接", "图片链接");
                            }
                        }).start(new DefaultResponse());
                break;
            case R.id.btn_pay:
                new HttpRequest<DefaultResponse>()
                        .with(this)
                        .setApiCode("_addindentnum_001")
                        .addParam("id", detail.getId())
                        .addParam("coupon_id", coupon_id)
                        .addParam("amount", "" + (detail.getPrice() - couponPrice))
                        .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                            @Override
                            public void onSuccess(BaseResponse response) {
                                try {
                                    String order_num = new JSONObject(response.data).optString("order_num");
                                    PayActivity.openByClass(ConfirmClassOrderActivity.this, detail.getPrice() - couponPrice);
                                } catch (JSONException e) {
                                    toast("返回数据异常");
                                }

                            }
                        }).start(new DefaultResponse());

                break;
            case R.id.layout_coupon:
                Bundle bundle1 = new Bundle();
                bundle1.putDouble("amount", detail.getPrice());
                startActivityForResult(CouponListActivity.class, bundle1, CHOOSE_COUPON);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == resultCode && requestCode == CHOOSE_COUPON) {
            Bundle extras1 = data.getExtras();
            couponPrice = extras1.getDouble("price", 0);
            coupon_id = extras1.getString("couponId", "");
            tvCoupon.setText("已使用1张");
            refreshPrice();
        }
    }

    public static final int CHOOSE_COUPON = 0x123;

    public void showShare(final String title, final String content, final String url, final String imgUrl) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                if ("Wechat".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                }
                if ("WechatMoments".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                }
                if ("QQ".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setTitleUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                }
                if ("QZone".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setTitleUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                }
            }
        });

        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                toast("分享成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                toast("分享失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                toast("分享取消");
            }
        });

        // 启动分享GUI
        oks.show(this);
    }
}
